package com.shuoyue.ycgk.ui.recruitment.newsinfo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.RecruitmentItem;
import com.shuoyue.ycgk.ui.search.SearchActivity;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NewNoticeActivity extends BaseMvpActivity<NoticeListContact.Presenter>
        implements NoticeListContact.View {

    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;
    @BindView(R.id.iv_right)
    ImageView ivRight;


    NoticeAdapter adapter;
    ListWithPage listWithPage;

    Integer modelId;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_sigle_list;
    }

    @Override
    protected void initView() {
        String title = getIntent().getStringExtra("title");
        if (title == null)
            pageTitle.setText("新公告");
        else
            pageTitle.setText(title);

        ivRight.setVisibility(View.VISIBLE);
        ivRight.setImageResource(R.mipmap.search);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 12), false));

        adapter = new NoticeAdapter(null);
        recyclerView.setAdapter(adapter);
        refreshlayout.setEnableLoadMore(false);

        refreshlayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getRecurimentList(modelId == -1 ? null : modelId, 1);
            refreshLayout.finishRefresh(1000);
        });
        refreshlayout.setOnLoadMoreListener(refreshLayout -> {
            mPresenter.getRecurimentList(modelId == -1 ? null : modelId, listWithPage.getCurrent() + 1);
            refreshLayout.finishLoadMore(1000);
        });
        registEmptyView(adapter);
        adapter.setOnItemChildClickListener((adapter, view, position) -> {
            RecruitmentItem recruitmentItem = (RecruitmentItem) adapter.getItem(position);
            startActivity(new Intent(mContext, RecurimentInfoActivity.class)
                    .putExtra("id", recruitmentItem.getId())
            );
        });
    }

    @Override
    public void initData() {
        super.initData();
        modelId = getIntent().getIntExtra("modelId", -1);
        mPresenter = new NoticeListContact.Presenter();
        mPresenter.attachView(this);
        mPresenter.getRecurimentList(modelId == -1 ? null : modelId, 1);
    }


    @Override
    public void setList(ListWithPage<RecruitmentItem> list) {
        listWithPage = list;
        if (list.getCurrent() == 1) {
            adapter.resetData(list.getRecords());
        } else {
            adapter.addData(list.getRecords());
        }
        refreshlayout.setEnableLoadMore(list.getCurrent() != list.getPages()&& list.getPages() > 0);
        refreshlayout.finishRefresh();
        refreshlayout.finishLoadMore();
    }



    @OnClick({R.id.back, R.id.iv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.iv_right:
                SearchActivity.start(mContext, 4, 0, "招考公告");

                break;
        }
    }
}
